package net.simflow.swf.analyzer.exec.stats.opt;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import net.simflow.swf.analyzer.exec.data.ServiceOverAbilityRecord;
import net.simflow.swf.analyzer.network.data.Flow;

public class FlowOverAbilityTimeAnalyzer {

	private static FlowOverAbilityTimeAnalyzer instance = new FlowOverAbilityTimeAnalyzer();

	private FlowOverAbilityTimeAnalyzer() {
	}

	public static FlowOverAbilityTimeAnalyzer getInstance() {
		return instance;
	}

	public HashMap<Flow, ServiceOverAbilityRecord> analyzeOverAbilityTime(
			Collection<? extends Flow> flowSet) {
		HashMap<Flow, ServiceOverAbilityRecord> map = new HashMap<Flow, ServiceOverAbilityRecord>();

		if (flowSet == null) {
			return map;
		}

		for (Flow flow : flowSet) {
			if (flow.getServiceOverAbilityRecord() != null) {
				map.put(flow, flow.getServiceOverAbilityRecord());
			}
		}

		return map;
	}

	public TreeMap<Integer, ArrayList<Flow>> analyzeOverAbilityTimeDistribution(
			Map<Flow, ServiceOverAbilityRecord> flowOverAbilityTimeMap) {
		TreeMap<Integer, ArrayList<Flow>> map = new TreeMap<Integer, ArrayList<Flow>>();

		if (flowOverAbilityTimeMap == null) {
			return map;
		}

		for (Entry<Flow, ServiceOverAbilityRecord> en : flowOverAbilityTimeMap
				.entrySet()) {
			Flow f = en.getKey();
			int time = en.getValue().getTime();
			ArrayList<Flow> a = map.get(time);
			if (a == null) {
				a = new ArrayList<Flow>();
				map.put(time, a);
			}
			a.add(f);
		}
		map.comparator();

		return map;
	}
}
